/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package co.com.edu.gobesco.controller;

import co.com.edu.gobesco.controller.exceptions.NonexistentEntityException;
import co.com.edu.gobesco.controller.exceptions.PreexistingEntityException;
import co.com.edu.gobesco.entityes.TipoPersona;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author: Edwin Chía
 * @Fecha: 20/10/2011
 */
public class TipoPersonaJpaController {

    public TipoPersonaJpaController() {
	emf = Persistence.createEntityManagerFactory("GobescoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
	return emf.createEntityManager();
    }

    public void create(TipoPersona tipoPersona) throws PreexistingEntityException, Exception {
	EntityManager em = null;
	try {
	    em = getEntityManager();
	    em.getTransaction().begin();
	    em.persist(tipoPersona);
	    em.getTransaction().commit();
	} catch (Exception ex) {
	    if (findTipoPersona(tipoPersona.getIdTipopersona()) != null) {
		throw new PreexistingEntityException("TipoPersona " + tipoPersona + " already exists.", ex);
	    }
	    throw ex;
	} finally {
	    if (em != null) {
		em.close();
	    }
	}
    }

    public void edit(TipoPersona tipoPersona) throws NonexistentEntityException, Exception {
	EntityManager em = null;
	try {
	    em = getEntityManager();
	    em.getTransaction().begin();
	    tipoPersona = em.merge(tipoPersona);
	    em.getTransaction().commit();
	} catch (Exception ex) {
	    String msg = ex.getLocalizedMessage();
	    if (msg == null || msg.length() == 0) {
		BigDecimal id = tipoPersona.getIdTipopersona();
		if (findTipoPersona(id) == null) {
		    throw new NonexistentEntityException("The tipoPersona with id " + id + " no longer exists.");
		}
	    }
	    throw ex;
	} finally {
	    if (em != null) {
		em.close();
	    }
	}
    }

    public void destroy(BigDecimal id) throws NonexistentEntityException {
	EntityManager em = null;
	try {
	    em = getEntityManager();
	    em.getTransaction().begin();
	    TipoPersona tipoPersona;
	    try {
		tipoPersona = em.getReference(TipoPersona.class, id);
		tipoPersona.getIdTipopersona();
	    } catch (EntityNotFoundException enfe) {
		throw new NonexistentEntityException("The tipoPersona with id " + id + " no longer exists.", enfe);
	    }
	    em.remove(tipoPersona);
	    em.getTransaction().commit();
	} finally {
	    if (em != null) {
		em.close();
	    }
	}
    }

    public List<TipoPersona> findTipoPersonaEntities() {
	return findTipoPersonaEntities(true, -1, -1);
    }

    public List<TipoPersona> findTipoPersonaEntities(int maxResults, int firstResult) {
	return findTipoPersonaEntities(false, maxResults, firstResult);
    }

    private List<TipoPersona> findTipoPersonaEntities(boolean all, int maxResults, int firstResult) {
	EntityManager em = getEntityManager();
	try {
	    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
	    cq.select(cq.from(TipoPersona.class));
	    Query q = em.createQuery(cq);
	    if (!all) {
		q.setMaxResults(maxResults);
		q.setFirstResult(firstResult);
	    }
	    return q.getResultList();
	} finally {
	    em.close();
	}
    }

    public TipoPersona findTipoPersona(BigDecimal id) {
	EntityManager em = getEntityManager();
	try {
	    return em.find(TipoPersona.class, id);
	} finally {
	    em.close();
	}
    }

    public int getTipoPersonaCount() {
	EntityManager em = getEntityManager();
	try {
	    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
	    Root<TipoPersona> rt = cq.from(TipoPersona.class);
	    cq.select(em.getCriteriaBuilder().count(rt));
	    Query q = em.createQuery(cq);
	    return ((Long) q.getSingleResult()).intValue();
	} finally {
	    em.close();
	}
    }

}
